package com.approval.approvalapp.index.dbsx.wzsp.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.NewNewWzSpBean;
import com.approval.approvalapp.bean.WzSpBean;
import com.approval.approvalapp.index.dbsx.wzsp.activity.WzspxqDbActivity;
import com.approval.approvalapp.index.dbsx.wzsp.activity.WzspxqYbActivity;
import com.approval.approvalapp.util.SpUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by louyulin on 2019/4/29.
 */

public class WzSpYbAdapter extends RecyclerView.Adapter<WzSpYbAdapter.Holder> {

    List<NewNewWzSpBean> wzSpYbBeanList;

    Context context;

    public void setWzSpYbBeanList(List<NewNewWzSpBean> wzSpYbBeanList) {
        this.wzSpYbBeanList = wzSpYbBeanList;
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_wzspyb, parent, false);
        Holder holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final Holder holder, final int position) {
        holder.tv1.setText(wzSpYbBeanList.get(position).getName() + "的物资领用");

        Long timeStamp = new Long(wzSpYbBeanList.get(position).getAdd_time()) * 1000;  //获取当前时间戳
        Date date = new Date(timeStamp);
        int dateresult = date.getYear() + 1900;
        Date date2 = new Date(System.currentTimeMillis());
        int currentResult = date2.getYear() + 1900;

        if (dateresult == currentResult) {
            //同年
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
            String timese = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
            holder.tv2.setText(timese);
        } else {
            //不同年
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM-dd HH:mm");
            String timese = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
            holder.tv2.setText(timese);
        }
        holder.tv3.setText("物资分类: " + wzSpYbBeanList.get(position).getType());
        holder.tv4.setText("申领物资: " + wzSpYbBeanList.get(position).getTitle_list());
        holder.tv5.setText("申领数量: " + wzSpYbBeanList.get(position).getCount() + "件");

        if (wzSpYbBeanList.get(position).getIf_use().equals("1")) {
            holder.tv6.setText("✓ 初审通过");
            holder.tv6.setTextColor(Color.parseColor("#1465FD"));
        } else if (wzSpYbBeanList.get(position).getIf_use().equals("2")){
            holder.tv6.setText("× 初审不通过");
            holder.tv6.setTextColor(Color.parseColor("#DE463A"));
        }else if (wzSpYbBeanList.get(position).getIf_use().equals("3")) {
            holder.tv6.setText("✓ 终审通过");
            holder.tv6.setTextColor(Color.parseColor("#1465FD"));
        }else {
            holder.tv6.setText("× 终审不通过");
            holder.tv6.setTextColor(Color.parseColor("#DE463A"));
        }

        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, WzspxqYbActivity.class);
                intent.putExtra("name",wzSpYbBeanList.get(position).getName());
                intent.putExtra("id",wzSpYbBeanList.get(position).getId());
                intent.putExtra("state",holder.tv6.getText().toString());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (wzSpYbBeanList == null) {
            return 0;
        } else {
            return wzSpYbBeanList.size();
        }
    }

    class Holder extends RecyclerView.ViewHolder {

        @Bind(R.id.tv1)
        TextView tv1;
        @Bind(R.id.tv2)
        TextView tv2;
        @Bind(R.id.tv3)
        TextView tv3;
        @Bind(R.id.tv4)
        TextView tv4;
        @Bind(R.id.tv5)
        TextView tv5;
        @Bind(R.id.tv6)
        TextView tv6;
        @Bind(R.id.root)
        LinearLayout root;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }
}
